<template>
  <el-select
    :v-model="selected"
    :value="valueTitle"
    placeholder
    :clearable="clearable"
    @clear="clearHandle"
    class="maxwidth"
    ref="selectBlur"
  >
    <!-- <el-input class="selectInput" v-model="filterText"></el-input> -->
    <el-option :value="valueTitle" :label="valueTitle" class="options">
      <el-tree
        id="tree-option"
        ref="selectTreeRef"
        :accordion="accordion"
        :data="metaData"
        :props="props"
        :node-key="props.value"
        :default-expanded-keys="defaultExpandedKey"
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        @node-click="handleNodeClick"
        @current-change="changeCurrentNode"
      ></el-tree>
    </el-option>
  </el-select>
</template>

<script>
export default {
  name: 'ElTreeSelect',
  model: {
    prop: 'selected',
    event: 'change'
  },
  props: {
    props: {
      type: Object,
      default: () => ({
        value: 'id',
        label: 'title',
        children: 'children'
      })
    },
    metaData: {
      type: Array,
      default: () => {
        return []
      }
    },
    selected: Number,
    value: {
      default: null
    },
    clearable: {
      type: Boolean,
      default: true
    },
    accordion: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: () => {
        return '请选择'
      }
    }
  },
  data() {
    return {
      valueId: null,
      valueTitle: '',
      defaultExpandedKey: [],
      filterText: ''
    }
  },
  watch: {
    value() {
      this.valueId = ''
      this.valueTitle = ''
      this.valueId = this.value
      this.initHandle()
    },
    filterText(val) {
      this.$refs.selectTreeRef.filter(val)
    }
  },
  mounted() {
    this.initHandle()
  },
  methods: {
    changeCurrentNode(node) {
      this.$emit('change', node)
    },
    initHandle() {
      if (this.valueId) {
        this.valueTitle = this.$refs.selectTreeRef.getNode(this.valueId).data.title
        this.$refs.selectTreeRef.setCurrentKey(this.valueId)
        this.defaultExpandedKey = [this.valueId]
      }
      this.initScroll()
    },
    // 初始化滚动条
    initScroll() {
      this.$nextTick(() => {
        const scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
        const scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
        scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
        scrollBar.forEach(ele => (ele.style.width = 0))
      })
    },
    // 切换选项
    handleNodeClick(node) {
      this.$refs.selectBlur.blur()
      this.valueTitle = node[this.props.label]
      this.valueId = node[this.props.value]
      this.$emit('getValue', this.valueId)
      this.defaultExpandedKey = []
    },
    clearHandle() {
      this.valueTitle = ''
      this.valueId = null
      this.defaultExpandedKey = []
      this.clearSelected()
      this.$emit('getValue', null)
      this.$emit('clearSelect')
    },
    // 清除选中
    clearSelected() {
      const allNode = document.querySelectorAll('#tree-option .el-tree-node')
      allNode.forEach(ele => ele.classList.remove('is-current'))
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    }
  }
}
</script>
<style scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  height: auto;
  max-height: 274px;
  padding: 0;
  overflow: hidden;
  overflow-y: auto;
}

.el-select-dropdown__item.selected {
  font-weight: normal;
}

.ul li >>> .el-tree .el-tree-node__content {
  height: auto;
  padding: 0 20px;
}

.el-tree-node__label {
  font-weight: normal;
}

.el-tree >>> .is-current .el-tree-node__label {
  color: #409eff;
  font-weight: 700;
}

.el-tree >>> .is-current .el-tree-node__children .el-tree-node__label {
  color: #606266;
  font-weight: normal;
}

.maxwidth {
  width: 100%;
}

.selectInput {
  padding: 0 5px;
  box-sizing: border-box;
}
</style>
